<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class categories extends MY_Model{
    public function __construct() {
        parent::__construct();
    }
    
    public function addCat($fields = array()){
        if(empty($fields)){
            return false;
        }
        $this->db->insert('categories',$fields);
        
        return $this->db->insert_id();
    }
    public function countArticleByCatId($cat_id){
        $query = $this->db->get_where('posts',array('cat_id'=>$cat_id));
        return $query->num_rows();
    }
    //get categories
    public function getCat($conditions=array()){
        $this->db->order_by('ordering');
        if(empty($conditions))
            $query = $this->db->get('categories');
        else
            $query = $this->db->get_where('categories',$conditions);
       
        if(isset($conditions['cat_id']))
            return $query->row();
        return $query->result();
    }
    
    public function getCat2(){
        $this->db->order_by('ordering');
        $this->db->where('cat_type !=','shop');
            $query = $this->db->get('categories');
       
        
        return $query->result();
    }//update category
    public function update_category($field=array(),$conditions = array())
    {
        if(empty($field) || empty($conditions)){
            
            return false;
        }
        $this->db->update('categories',$field,$conditions);
        return $this->db->affected_rows();
    }
    
    //delete category
    public function delete_category($category_id=false)
    {
        if($category_id){
            $this->db->where('cat_id',$category_id);
            $this->db->delete('categories');
        }        
        return $this->db->affected_rows();
        
    }
}
?>
